﻿using DFGK_WMS.IRepository;
using DFGK_WMS.Model.Models;
using DFGK_WMS.Repository;
using DFGK_WMS.WebAPI.Controllers.Result;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace DFGK_WMS.WebAPI.Controllers.物料管理
{
    /// <summary>
    /// 物料批次
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMaterialKeyController : ControllerBase
    {
        public TMaterialKeyController(ITMaterialKeyRepository tMaterialKey)
        {
            TMaterialKey = tMaterialKey;
        }

        public ITMaterialKeyRepository TMaterialKey { get; }

        /// <summary>
        /// 添加物料批次
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddTMaterialKey(TMaterialKey m)
        {
            bool IsOK = TMaterialKey.AddTMaterialKey(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "添加成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "添加失败！编码已存在！", "500"));
        }

        /// <summary>
        /// 删除物料批次
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DelTMaterialKey(int mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = TMaterialKey.DelTMaterialKey(mid);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "删除成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "删除失败！", "500"));

        }

        /// <summary>
        /// 修改批次
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdTMaterialKey(TMaterialKey m)
        {
            if(m.Id<=0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            bool IsOK = TMaterialKey.UpdTMaterialKey(m);
            if (IsOK)
            {
                return Ok(BaseResult.Result(true, "修改成功！", "200"));
            }
            return Ok(BaseResult.Result(false, "修改失败！编码已存在！", "500"));

        }

        /// <summary>
        /// 根据主键获取对象
        /// </summary>
        /// <param name="mid"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTMaterialKeyById(int mid)
        {
            if (mid <= 0)
            {
                return Ok(BaseResult.Result("400", "Id有误！"));
            }
            var rule = TMaterialKey.GetTMaterialKeyById(mid);
            if (rule != null)
            {
                return Ok(BaseResult.Result(rule, "获取成功", "200"));
            }
            return Ok(BaseResult.Result(null, "获取失败！Id可能有误或已被删除！", "500"));

        }

        /// <summary>
        /// 显示物料批次列表
        /// </summary>
        /// <param name="mname"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetTMaterialKeyList(string? mname,int pageIndex=1,int pageSize=10)
        {
            if(pageIndex<=0 || pageSize<=0)
            {
                return Ok(BaseResult.Result("400", "分页条件有误！"));
            }
            int totalCount;
            var list = TMaterialKey.GetTMaterialKeyList(mname, pageIndex, pageSize, out totalCount);
            if (list != null)
            {
                return Ok(BaseResult.Result(list, "查询成功！", "200", totalCount));
            }
            return Ok(BaseResult.Result(null, "查询失败！", "500", 0));

        }

        /// <summary>
        /// 根据物料id查询物料批次
        /// </summary>
        /// <param name="materialId"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetAllBatchNOSelectData(int materialId)
        {
            if (materialId <= 0)
            {
                return Ok(BaseResult.Result(null, "id有误！", "500", 0));
            }
            List<TMaterialKey> list =  this.TMaterialKey.GetMaterialKeyList(m=>m.MaterialId==materialId);

            if (list == null || list.Count == 0)
            {
                return Ok(BaseResult.Result(null, "查询失败！", "500", 0));
            }
            var batchNOList = list.Select(t => new { Id = t.Id, BatchNO = t.BatchNo }).ToList();
            return Ok(BaseResult.Result(batchNOList, "查询成功！", "200", batchNOList.Count()));
        }

        MemoryStream ProcessExcelData()
        {
            //模块从表中读取的数据
            List<TMaterialKey> list = new List<TMaterialKey>();
            list = TMaterialKey.GetTmaterExcel();
            // 已经读取出了正确的数据后
            //创建Excel对象把数据赋值到对象中
            IWorkbook workbook = new XSSFWorkbook(); // Excel对象
            ISheet sheet1 = workbook.CreateSheet("物料批次");  // 创建Excel中的表对象
                                                           //表头
            IRow row0 = sheet1.CreateRow(0);  // 在sheet1表格中创建第0行
            row0.CreateCell(0).SetCellValue("物料名称"); // 在sheet1表格中第0行创建第0列，并且在给这个单元格赋值
            row0.CreateCell(1).SetCellValue("物料号");
            row0.CreateCell(2).SetCellValue("批次号");
            row0.CreateCell(3).SetCellValue("质量状态");
            row0.CreateCell(4).SetCellValue("供应商");
            row0.CreateCell(5).SetCellValue("生产厂家");
            row0.CreateCell(6).SetCellValue("生产日期");
            row0.CreateCell(7).SetCellValue("出厂日期");
            row0.CreateCell(8).SetCellValue("有效期");
            row0.CreateCell(9).SetCellValue("质检日期");
            row0.CreateCell(10).SetCellValue("货主");
            row0.CreateCell(11).SetCellValue("创建时间");
            var rowIndex = 1;
            foreach (var item in list)
            {
                var st = "";
                if (item.QualityState == 0)
                {
                    st = "待检";
                }
                if (item.QualityState == 1)
                {
                    st = "合格";
                }
                if (item.QualityState == 2)
                {
                    st = "不合格";
                }
                IRow row = sheet1.CreateRow(rowIndex);
                row.CreateCell(0).SetCellValue(item.MaterialName);
                row.CreateCell(1).SetCellValue(item.MaterialCode);
                row.CreateCell(2).SetCellValue(item.BatchNo);
                row.CreateCell(3).SetCellValue(st);
                row.CreateCell(4).SetCellValue(item.SupplierName);
                row.CreateCell(5).SetCellValue(item.ProduceFactory);
                row.CreateCell(6).SetCellValue(item.ProductDate.ToString());
                row.CreateCell(7).SetCellValue(item.OutFactoryDate.ToString());
                row.CreateCell(8).SetCellValue(item.ExpireDate.ToString());
                row.CreateCell(9).SetCellValue(item.Qadate.ToString());
                row.CreateCell(10).SetCellValue(item.OwnerName);
                row.CreateCell(11).SetCellValue(item.CreateDate.ToString());
                rowIndex++;
            }
            MemoryStream ms = new MemoryStream();
            workbook.Write(ms);

            return ms;
        }
        /// <summary>
        /// 导出物料批次
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public FileContentResult DownTmater()
        {
            MemoryStream ms = ProcessExcelData();
            byte[] fileBytes = ms.ToArray();
            return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, "物料批次.xlsx"); //关键语句
        }
    }
}
